package com.whm.comtest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.whm.comtest.ftp.FtpUtil;
import com.whm.comtest.sms.SmsUtil;
import com.whm.comtest.sms.SmsUtil.SmsListener;

public class ComTestActivity extends Activity implements SmsListener    {
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnSendData = (Button)findViewById(R.id.id_send_data);
        btnSendData.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
          /*      SmsUtil.sendSms(ComTestActivity.this, ((EditText)findViewById(R.id.et_phone_num)).getText().toString()
                        , ((EditText)findViewById(R.id.id_msg_sent)).getText().toString());*/
            	
            	handleSms("");
            }
            
        });
        
        SmsUtil.setSmsListener(this);
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onConfigurationChanged(android.content.res.Configuration)
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void handleSms(String strMessage) {
        // TODO Auto-generated method stub
        ((TextView)findViewById(R.id.id_msg_received)).setText(strMessage);
        
		// FtpUtil.upLoad("/sdcard/aa.jpg", "www.ningshang99.com", 21,
		// "ubuntu","gyxmmb@23" );
		// FtpUtil.downLoad("/sdcard/abcd.txt", "www.ningshang99.com", 21,
		// "ubuntu","gyxmmb@23" );
		new AsyncTask<Void, Integer, Void>  () {

			ProgressDialog  m_progressDialog ;
			FtpUtil.ProgressListener m_listener = new FtpUtil.ProgressListener() {
				
				@Override
				public void notifyProgress(int nProgress) {
					// TODO Auto-generated method stub
					publishProgress(nProgress);
					
				}
			};
			
			@Override
			protected Void doInBackground(Void... a ) {
				// TODO Auto-generated method stub
				FtpUtil.downLoad("/sdcard/abcd.txt", "www.ningshang99.com", 21,
						"ubuntu", "gyxmmb@23", m_listener);
				
				/*FtpUtil.upLoad("/sdcard/aa.jpg", "www.ningshang99.com", 21,
			         "ubuntu","gyxmmb@23", m_listener );*/
				return null;
			}

			/* (non-Javadoc)
			 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
			 */
			@Override
			protected void onPostExecute(Void result) {
				// TODO Auto-generated method stub
				m_progressDialog.setProgress(100);
				m_progressDialog.dismiss();
			}

			/* (non-Javadoc)
			 * @see android.os.AsyncTask#onPreExecute()
			 */
			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				m_progressDialog = new ProgressDialog(ComTestActivity.this) ;
				m_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				m_progressDialog.setTitle("");
				m_progressDialog.setMessage("transfering sdp data");
				m_progressDialog.setCancelable(false);
				m_progressDialog.setProgress(0);
				m_progressDialog.show();
			}

			/* (non-Javadoc)
			 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
			 */
			@Override
			protected void onProgressUpdate(Integer... values) {
				// TODO Auto-generated method stub
				m_progressDialog.setProgress(values[0].intValue());
			}
			
			
		}.execute(null);
      
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onStart()
     */
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        
        
        SmsUtil.startListen(this);
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onStop()
     */
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        
        SmsUtil.stopListen();
        
        
    }
}